package com.Touch.TouchSimulator
{
	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.events.TouchEvent;
	import flash.geom.Point;

	public class SimulateTouchAppliedObj 
	{
		public var isExist:Boolean;//TUIO需要
		
		private var _local_ip:String;
		
		private var _local_id:int;
		
		private var _id:int;
		
		private var disobj:DisplayObject;
		
		private var stage:Stage;
		
		private var oldPoint:Point;
		
		public function SimulateTouchAppliedObj(l_id:int,_stage:Stage,_lip:String,_lid:int)
		{
			_id=l_id;
			stage=_stage;
			_local_ip=_lip;
			_local_id=_lid;
			isExist=true;
		}
		
		public function get id():int{
			return _id;
		}
		
		public function get local_ip():String{
			return _local_ip;
		}
		
		public function get local_id():int{
			return _local_id;
		}
		
		public function TouchOn(_x:Number,_y:Number,_disobj:DisplayObject):void{
			if(_disobj==null){
				//对象为空则对舞台触发事件
				stage.dispatchEvent(new TouchEvent(TouchEvent.TOUCH_BEGIN,true,false,_id,true,_x,_y,1,1,1,null,false,false,false));
			}else{
				disobj=_disobj;
				var localPoint:Point = disobj.globalToLocal(new Point(_x, _y));
				disobj.dispatchEvent(new TouchEvent(TouchEvent.TOUCH_BEGIN,true,false,_id,true,localPoint.x,localPoint.y,1,1,1,null,false,false,false));
				disobj.dispatchEvent(new TouchEvent(TouchEvent.TOUCH_OVER,true,false,_id,true,localPoint.x,localPoint.y,1,1,1,null,false,false,false));
			}
			oldPoint=new Point(_x,_y);
		}
		
		public function TouchMove(_x:Number,_y:Number,_disobj:DisplayObject):void{
			var _p:Point=new Point(_x, _y);
			if(disobj==_disobj){
				if(disobj==null){
					//对象为空则对舞台触发事件
					stage.dispatchEvent(new TouchEvent(TouchEvent.TOUCH_MOVE,true,false,_id,true,_x,_y,1,1,1,null,false,false,false));
				}else{
					//两者相同则对其执行
					var localPoint:Point = disobj.globalToLocal(_p);
					disobj.dispatchEvent(new TouchEvent(TouchEvent.TOUCH_MOVE,true,false,_id,true,localPoint.x,localPoint.y,1,1,1,null,false,false,false));
				}
			}else{
				
				if(disobj!=null){
					var disPoint:Point=disobj.globalToLocal(_p);
					disobj.dispatchEvent(new TouchEvent(TouchEvent.TOUCH_OUT,true,false,_id,true,disPoint.x,disPoint.y,1,1,1,null,false,false,false));
				}
				if(_disobj!=null){
					var _disPoint:Point=_disobj.globalToLocal(_p);
					_disobj.dispatchEvent(new TouchEvent(TouchEvent.TOUCH_OVER,true,false,_id,true,_disPoint.x,_disPoint.y,1,1,1,null,false,false,false));
					_disobj.dispatchEvent(new TouchEvent(TouchEvent.TOUCH_MOVE,true,false,_id,true,_disPoint.x,_disPoint.y,1,1,1,null,false,false,false));
				}
				disobj=_disobj;
			}
			oldPoint.x=_x;
			oldPoint.y=_y;
		}
		
		public function TouchEnd(_x:Number,_y:Number,_disobj:DisplayObject):void{
			var _p:Point=new Point(_x, _y);
			if(disobj==_disobj){
				if(disobj==null){
					//对象为空则对舞台触发事件
					stage.dispatchEvent(new TouchEvent(TouchEvent.TOUCH_END,true,false,_id,true,_x,_y,1,1,1,null,false,false,false));
				}else{
					//两者相同则对其执行
					var localPoint:Point = disobj.globalToLocal(_p);
					disobj.dispatchEvent(new TouchEvent(TouchEvent.TOUCH_END,true,false,_id,true,localPoint.x,localPoint.y,1,1,1,null,false,false,false));
				}
			}else{
				
				if(disobj!=null){
					var disPoint:Point=disobj.globalToLocal(_p);
					disobj.dispatchEvent(new TouchEvent(TouchEvent.TOUCH_OUT,true,false,_id,true,disPoint.x,disPoint.y,1,1,1,null,false,false,false));
				}
				if(_disobj!=null){
					var _disPoint:Point=_disobj.globalToLocal(_p);
					_disobj.dispatchEvent(new TouchEvent(TouchEvent.TOUCH_END,true,false,_id,true,_disPoint.x,_disPoint.y,1,1,1,null,false,false,false));
				}
				disobj=_disobj;
			}
		}
		
		//tuio用
		public function TouchRemove():void{
			if(disobj==null){
				stage.dispatchEvent(new TouchEvent(TouchEvent.TOUCH_END,true,false,_id,true,oldPoint.x,oldPoint.y,1,1,1,null,false,false,false));
			}else{
				var disPoint:Point=disobj.globalToLocal(oldPoint);
				disobj.dispatchEvent(new TouchEvent(TouchEvent.TOUCH_END,true,false,_id,true,disPoint.x,disPoint.y,1,1,1,null,false,false,false));
			}
		}
		
		/*   旧版本  2013 7 4 之前   */
		/*
		public function on(_x:Number,_y:Number,_disobj:DisplayObject):void{
			if(_disobj==null){
				//对象为空则对舞台触发事件
				stage.dispatchEvent(new TouchEvent(TouchEvent.TOUCH_BEGIN,true,false,id,false,_x,_y,1,1,1,null,false,false,false));
			}else{
				disobj=_disobj;
				var localPoint:Point = disobj.globalToLocal(new Point(_x, _y));
				disobj.dispatchEvent(new TouchEvent(TouchEvent.TOUCH_BEGIN,true,false,id,true,localPoint.x,localPoint.y,1,1,1,null,false,false,false));
				disobj.dispatchEvent(new TouchEvent(TouchEvent.TOUCH_OVER,true,false,id,true,localPoint.x,localPoint.y,1,1,1,null,false,false,false));
			}
		}
		public function up(_x:Number,_y:Number,_disobj:DisplayObject):void{
			if(disobj==null){
				if(_disobj==null){
					//均为空则对舞台触发事件
					stage.dispatchEvent(new TouchEvent(TouchEvent.TOUCH_END,true,false,id,true,_x,_y,1,1,1,null,false,false,false));
					return;
				}
				disobj=_disobj
			}
			if(disobj==_disobj){
				var localPoint:Point = disobj.globalToLocal(new Point(_x, _y));
				disobj.dispatchEvent(new TouchEvent(TouchEvent.TOUCH_END,true,false,id,true,localPoint.x,localPoint.y,1,1,1,null,false,false,false));
				return;
			}			if(disobj!=_disobj){
				var _localPoint:Point=_disobj.globalToLocal(new Point(_x,_y));
				_disobj.dispatchEvent(new TouchEvent(TouchEvent.TOUCH_END,true,false,id,true,_localPoint.x,_localPoint.y,1,1,1,null,false,false,false));
			}
		}
		public function updata(_x:Number,_y:Number,_disobj:DisplayObject):void{
			if(disobj==null){
				if(_disobj==null){
					//均为空则对舞台触发事件
					stage.dispatchEvent(new TouchEvent(TouchEvent.TOUCH_END,true,false,id,true,_x,_y,1,1,1,null,false,false,false));
					return;
				}
				disobj=_disobj
				//新对象存在  而旧对象不存在  对新对象触发移入事件
				var localPoint_1:Point = disobj.globalToLocal(new Point(_x, _y));
				disobj.dispatchEvent(new TouchEvent(TouchEvent.TOUCH_OVER,true,false,id,true,localPoint_1.x,localPoint_1.y,1,1,1,null,false,false,false));
			}else if(disobj!=_disobj){
				//如果旧对象存在而且与新对象不一样则对旧对象移出事件  新对象（存在）移入
				var localPoint_2:Point = disobj.globalToLocal(new Point(_x, _y));
				disobj.dispatchEvent(new TouchEvent(TouchEvent.TOUCH_OUT,true,false,id,true,localPoint_2.x,localPoint_2.y,1,1,1,null,false,false,false));
				if(_disobj!=null){
					var localPoint_3:Point = _disobj.globalToLocal(new Point(_x, _y));
					_disobj.dispatchEvent(new TouchEvent(TouchEvent.TOUCH_OVER,true,false,id,true,localPoint_3.x,localPoint_3.y,1,1,1,null,false,false,false));
				}
				disobj=_disobj;
			}else{
				//仍然在原有的对象上
				var localPoint_4:Point = disobj.globalToLocal(new Point(_x, _y));
				disobj.dispatchEvent(new TouchEvent(TouchEvent.TOUCH_MOVE,true,false,id,true,localPoint_4.x,localPoint_4.y,1,1,1,null,false,false,false));
			}
		}
		*/
	}
}